/**  
 * Project Name:api-webhook  
 * File Name:ChsiService.java  
 * Package Name:com.example.service.webhook.business.chsi.service  
 * Date:2016年7月26日下午4:07:19  
 * Copyright (c) 2016, yuandong@51dojo.com All Rights Reserved.  
 *  
*/

package com.example.service.webhook.business.zhengxin.service;

import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;

import com.example.service.webhook.business.zhengxin.api.ZhengxinClient;
import com.example.service.webhook.business.zhengxin.billitem.ZhengxinTask;
import com.example.service.webhook.business.zhengxin.dto.CreditBaseInfo;
import com.example.service.webhook.business.zhengxin.dto.CreditCardRecordDetailAnalyze;
import com.example.service.webhook.business.zhengxin.dto.CreditInformation;
import com.example.service.webhook.business.zhengxin.dto.CreditRecordDetail;
import com.example.service.webhook.business.zhengxin.dto.CreditRecordSummary;
import com.example.service.webhook.business.zhengxin.dto.HousingLoanRecordDetailAnalyze;
import com.example.service.webhook.business.zhengxin.dto.OtherLoanRecordDetailAnalyze;
import com.example.service.webhook.business.zhengxin.dto.PublicInformationDetail;
import com.example.service.webhook.business.zhengxin.dto.QueryInformationDetail;
import com.example.service.webhook.business.zhengxin.entity.CreditBaseInfoEntity;
import com.example.service.webhook.business.zhengxin.entity.CreditCardRecordDetailAnalyzeEntity;
import com.example.service.webhook.business.zhengxin.entity.CreditRecordDetailEntity;
import com.example.service.webhook.business.zhengxin.entity.CreditRecordSummaryEntity;
import com.example.service.webhook.business.zhengxin.entity.HousingLoanRecordDetailAnalyzeEntity;
import com.example.service.webhook.business.zhengxin.entity.OtherLoanRecordDetailAnalyzeEntity;
import com.example.service.webhook.business.zhengxin.entity.PublicInformationEntity;
import com.example.service.webhook.business.zhengxin.entity.QueryInformationEntity;
import com.example.service.webhook.business.zhengxin.repository.CreditBaseInfoRepository;
import com.example.service.webhook.business.zhengxin.repository.CreditCardRecordDetailAnalyzeRepository;
import com.example.service.webhook.business.zhengxin.repository.CreditRecordDetailRepository;
import com.example.service.webhook.business.zhengxin.repository.CreditRecordSummaryRepository;
import com.example.service.webhook.business.zhengxin.repository.HousingLoanRecordDetailAnalyzeRepository;
import com.example.service.webhook.business.zhengxin.repository.OtherLoanRecordDetailAnalyzeRepository;
import com.example.service.webhook.business.zhengxin.repository.PublicInformationRepository;
import com.example.service.webhook.business.zhengxin.repository.QueryInformationRepository;
import com.example.service.webhook.util.DateUtil;

/**
 * 
 * ClassName: ZhengxinService    
 * date: 2017年1月11日 下午7:46:08   
 * @author yuandong  
 * @version   
 * @since JDK 1.6
 */
@Service
public class ZhengxinService {

	private static final Logger LOGGER = LoggerFactory.getLogger(ZhengxinService.class);

	@Autowired
	private ThreadPoolTaskExecutor taskExecutor;
	@Autowired
	private ZhengxinClient zhengxinClient;
	@Autowired
	private CreditBaseInfoRepository creditBaseInfoRepository;
	@Autowired
	private CreditRecordSummaryRepository creditRecordSummaryRepository;
	@Autowired
	private CreditRecordDetailRepository creditRecordDetailRepository;
	@Autowired
	private PublicInformationRepository publicInformationRepository;
	@Autowired
	private QueryInformationRepository queryInformationRepository;
	@Autowired
	private CreditCardRecordDetailAnalyzeRepository creditCardRecordDetailAnalyzeRepository;
	@Autowired
	private HousingLoanRecordDetailAnalyzeRepository housingLoanRecordDetailAnalyzeRepository;
	@Autowired
	private OtherLoanRecordDetailAnalyzeRepository otherLoanRecordDetailAnalyzeRepository;

	public void fetchBill(final ZhengxinTask task) {

		// 这里交给线程池处理，防止下面的业务处理时间太长，导致超时。
		// 超时会导致魔蝎数据进行重试，会收到重复的回调请求

		taskExecutor.execute(new Runnable() {
			@Override
			public void run() {
				try {
					Date startTime = DateUtil.getCurrentDate();
					CreditInformation creditInformation = zhengxinClient.getCreditInfo(task.getTaskId(),
							task.getReportNo());
					if (creditInformation != null) {
						
						//保存基本信息
						CreditBaseInfo creditBaseInfo = creditInformation.getCreditBaseInfo();
						CreditBaseInfoEntity creditBaseInfoEntity = new CreditBaseInfoEntity();
						BeanUtils.copyProperties(creditBaseInfo, creditBaseInfoEntity);
						creditBaseInfoEntity.setUserId(task.getUserId());
						CreditBaseInfoEntity isExist = creditBaseInfoRepository.getCreditBaseInfo(task.getUserId(), task.getMappingId(), task.getReportNo());
						if(isExist!=null){
							creditBaseInfoEntity.setId(isExist.getId());
							creditBaseInfoEntity.setCreateTime(isExist.getCreateTime());
							creditBaseInfoEntity.setLastModifyTime(DateUtil.getCurrentDate());
							creditBaseInfoRepository.updateCreditBaseInfo(creditBaseInfoEntity);
						}else{
							creditBaseInfoRepository.saveCreditBaseInfo(creditBaseInfoEntity);
						}
						
					   //人行征信报告信贷记录信息概要数组
						if(creditInformation.getCreditRecordSummaries()!=null&&!creditInformation.getCreditRecordSummaries().isEmpty()){
							creditRecordSummaryRepository.deleteCreditRecordSummaryEntity(task.getUserId(), task.getMappingId(), task.getReportNo());
						    for(CreditRecordSummary creditRecordSummary:creditInformation.getCreditRecordSummaries()){
						    	CreditRecordSummaryEntity creditRecordSummaryEntity = new CreditRecordSummaryEntity();
								BeanUtils.copyProperties(creditRecordSummary, creditRecordSummaryEntity);
								creditRecordSummaryEntity.setUserId(task.getUserId());
								creditRecordSummaryRepository.saveCreditRecordSummaryEntity(creditRecordSummaryEntity);
						    }
						}
						
						//信贷记录详细信息数组
						if(creditInformation.getCreditRecordDetails()!=null&&!creditInformation.getCreditRecordDetails().isEmpty()){
							creditRecordDetailRepository.deleteCreditRecordDetailEntity(task.getUserId(), task.getMappingId(), task.getReportNo());
						    for(CreditRecordDetail creditRecordDetail:creditInformation.getCreditRecordDetails()){
						    	CreditRecordDetailEntity creditRecordDetailEntity = new CreditRecordDetailEntity();
								BeanUtils.copyProperties(creditRecordDetail, creditRecordDetailEntity);
								creditRecordDetailEntity.setUserId(task.getUserId());
								creditRecordDetailRepository.saveCreditRecordDetailEntity(creditRecordDetailEntity);
						    }
						}
						
						//公共记录的结构数组
						if(creditInformation.getPublicInformationDetails()!=null&&!creditInformation.getPublicInformationDetails().isEmpty()){
							publicInformationRepository.deletePublicInformationEntity(task.getUserId(), task.getMappingId(), task.getReportNo());
						    for(PublicInformationDetail publicInformationDetail:creditInformation.getPublicInformationDetails()){
						    	PublicInformationEntity publicInformationEntity = new PublicInformationEntity();
								BeanUtils.copyProperties(publicInformationDetail, publicInformationEntity);
								publicInformationEntity.setUserId(task.getUserId());
								publicInformationRepository.savePublicInformationEntity(publicInformationEntity);
						    }
						}
						
						//查询记录数组, 这部分包含信用报告最近2年内被查询的记录	
						if(creditInformation.getQueryInformationDetails()!=null&&!creditInformation.getQueryInformationDetails().isEmpty()){
							queryInformationRepository.deletePublicInformationEntity(task.getUserId(), task.getMappingId(), task.getReportNo());
						    for(QueryInformationDetail queryInformationDetail:creditInformation.getQueryInformationDetails()){
						    	QueryInformationEntity queryInformationEntity = new QueryInformationEntity();
								BeanUtils.copyProperties(queryInformationDetail, queryInformationEntity);
								queryInformationEntity.setUserId(task.getUserId());
								queryInformationRepository.savePublicInformationEntity(queryInformationEntity);
						    }
						}
						
						
						//信用卡记录详细信息析记录数组
						if(creditInformation.getCreditCardRecordDetailAnalyzes()!=null&&!creditInformation.getCreditCardRecordDetailAnalyzes().isEmpty()){
							creditCardRecordDetailAnalyzeRepository.deleteCreditCardRecordDetailAnalyze(task.getUserId(), task.getMappingId(), task.getReportNo());
						    for(CreditCardRecordDetailAnalyze creditCardRecordDetailAnalyze:creditInformation.getCreditCardRecordDetailAnalyzes()){
						    	CreditCardRecordDetailAnalyzeEntity creditCardRecordDetailAnalyzeEntity = new CreditCardRecordDetailAnalyzeEntity();
								BeanUtils.copyProperties(creditCardRecordDetailAnalyze, creditCardRecordDetailAnalyzeEntity);
								creditCardRecordDetailAnalyzeEntity.setUserId(task.getUserId());
								creditCardRecordDetailAnalyzeRepository.saveCreditCardRecordDetailAnalyze(creditCardRecordDetailAnalyzeEntity);
						    }
						}
						
						//购房贷款记录详细信息析记录数组
						if(creditInformation.getHousingLoanRecordDetailAnalyzes()!=null&&!creditInformation.getHousingLoanRecordDetailAnalyzes().isEmpty()){
							housingLoanRecordDetailAnalyzeRepository.deleteHousingLoanRecordDetailAnalyze(task.getUserId(), task.getMappingId(), task.getReportNo());
						    for(HousingLoanRecordDetailAnalyze housingLoanRecordDetailAnalyze:creditInformation.getHousingLoanRecordDetailAnalyzes()){
						    	HousingLoanRecordDetailAnalyzeEntity housingLoanRecordDetailAnalyzeEntity = new HousingLoanRecordDetailAnalyzeEntity();
								BeanUtils.copyProperties(housingLoanRecordDetailAnalyze, housingLoanRecordDetailAnalyzeEntity);
								housingLoanRecordDetailAnalyzeEntity.setUserId(task.getUserId());
								housingLoanRecordDetailAnalyzeRepository.saveHousingLoanRecordDetailAnalyze(housingLoanRecordDetailAnalyzeEntity);
						    }
						}
						
						//其他贷款记录详细信息析记录数组
						if(creditInformation.getOtherLoanRecordDetailAnalyzes()!=null&&!creditInformation.getOtherLoanRecordDetailAnalyzes().isEmpty()){
							otherLoanRecordDetailAnalyzeRepository.deleteOtherLoanRecordDetailAnalyze(task.getUserId(), task.getMappingId(), task.getReportNo());
						    for(OtherLoanRecordDetailAnalyze otherLoanRecordDetailAnalyze:creditInformation.getOtherLoanRecordDetailAnalyzes()){
						    	OtherLoanRecordDetailAnalyzeEntity otherLoanRecordDetailAnalyzeEntity = new OtherLoanRecordDetailAnalyzeEntity();
								BeanUtils.copyProperties(otherLoanRecordDetailAnalyze, otherLoanRecordDetailAnalyzeEntity);
								otherLoanRecordDetailAnalyzeEntity.setUserId(task.getUserId());
								otherLoanRecordDetailAnalyzeRepository.saveOtherLoanRecordDetailAnalyze(otherLoanRecordDetailAnalyzeEntity);
						    }
						}
						
						
						Long taskTime = DateUtil.distance(startTime, DateUtil.getCurrentDate());
						LOGGER.info(String.format("征信任务耗时 %s,taskId=%s,reportNo=%s", taskTime.toString(), task.getTaskId(),
								task.getReportNo()));
					}

				} catch (Exception e) {
					LOGGER.error("zhengxin callback error，mappingId="+task.getMappingId(),e);
				}

			}
		}

		);

	}

}
